<template>
   <div class="app-container">
      <a-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px" layout="inline">
         <a-form-item label="姓名" field="name">
           <a-input
               size="small"
               v-model="queryParams.name"
               placeholder="请输入姓名"
               clearable
               @keyup.enter="handleQuery"
           />
         </a-form-item>
        <a-form-item label="户籍" field="hukouAddress">
          <a-input
              size="small"
              v-model="queryParams.hukouAddress"
              placeholder="请输入户籍"
              clearable
              @keyup.enter="handleQuery"
          />
        </a-form-item>
         <a-form-item>
            <a-button type="primary" class="button-margin-right" @click="handleQuery">
              <template #icon>
                <icon-Search/>
              </template>
              搜索
            </a-button>
            <a-button  @click="resetQuery">
              <template #icon>
                <icon-refresh />
              </template>
              重置
            </a-button>
         </a-form-item>
      </a-form>

      <a-row :gutter="10" class="mb8">
         <a-col :span="1.5">
            <a-button
               type="primary"
               plain
               @click="handleAdd"
               v-hasPermi="['system:dict:add']"
            >
              <template #icon>
                <icon-plus/>
              </template>
              新增
            </a-button>
         </a-col>
         <a-col :span="1.5">
            <a-button
                status="success"
                type="primary"
               plain
               :disabled="single"
               @click="handleUpdate"
               v-hasPermi="['system:dict:edit']"
            >
              <template #icon>
                <icon-edit/>
              </template>
              修改
            </a-button>
         </a-col>
         <a-col :span="1.5">
            <a-button
                status="danger"
                type="primary"
               plain
               :disabled="multiple"
               @click="handleDelete"
               v-hasPermi="['system:dict:remove']"
            >
              <template #icon>
                <icon-delete/>
              </template>
              删除
            </a-button>
         </a-col>
         <a-col :span="1.5">
            <a-button
               status="warning"
               plain
               @click="handleClose"
            >
              <template #icon>
                <icon-close/>
              </template>
              关闭
            </a-button>
         </a-col>
         <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </a-row>

      <a-table v-loading="loading" :data="dataList"
               row-key="id" :row-selection="rowSelection"
               v-model:selectedKeys="selectedIds" :pagination="false"
               @selection-change="handleSelectionChange">
        <template #columns>
          <a-table-column title="姓名" align="center" data-index="name" />
          <a-table-column data-index="gender" title="性别">
            <template #cell="{record}">
              <dict-tag :options="sys_user_sex" :value="record.gender" />
            </template>
          </a-table-column>
          <a-table-column title="与户主关系" align="center" data-index="relation"/>
          <a-table-column title="出生日期" align="center" data-index="birthday" />
          <a-table-column title="身份证号码" align="center" data-index="idCard" />
          <a-table-column title="联系方式" align="center" data-index="contact"/>
          <a-table-column title="户籍" align="center" data-index="hukouAddress"/>
          <a-table-column data-index="populationType" title="人口类型" width="100">
            <template #cell="{record}">
              <dict-tag :options="bis_peo_type" :value="record.populationType" />
            </template>
          </a-table-column>
          <a-table-column title="创建时间" align="center" data-index="createTime" width="170"/>
          <a-table-column title="操作" align="center" width="180" class-name="small-padding fixed-width">
            <template #cell="{record}">
              <a-button
                  type="text"
                  @click="handleUpdate(record)"
              >
                <icon-edit/>
                修改
              </a-button>
              <a-button
                  type="text"
                  @click="handleDelete(record)"
              >
                <icon-delete/>
                删除
              </a-button>
            </template>
          </a-table-column>
        </template>

      </a-table>

     <!--分页-->
     <div style="margin-top: 30px;float: right">
       <a-pagination :total="total" show-total show-jumper show-page-size
                     v-model:current="queryParams.pageNum"
                     v-model:page-size="queryParams.pageSize"
                     @change="onPageChange"
                     @page-size-change="onPageSizeChange"
       />
     </div>
      <!-- 添加或修改参数配置对话框 -->
     <a-drawer :visible="dictDrawerVisible" @ok="handleDictOk" @cancel="handleDictCancel" unmountOnClose :width="540">
       <template #title>
         <h4>{{title}}</h4>
       </template>
       <a-form ref="dataRef" :model="form" :rules="rules" label-width="80px">
         <a-form-item label="姓名" field="name">
           <a-input v-model="form.name" placeholder="请输入姓名"/>
         </a-form-item>
         <a-form-item label="性别" field="gender">
           <a-select v-model="form.gender" placeholder="请选择">
             <a-option
                 v-for="dict in sys_user_sex"
                 :key="dict.value"
                 :label="dict.label"
                 :value="dict.value"
             ></a-option>
           </a-select>
         </a-form-item>
         <a-form-item label="与户主关系" field="relation">
           <a-input v-model="form.relation" placeholder="请输入与户主关系" />
         </a-form-item>
         <a-form-item label="出生日期" field="birthday">
           <a-date-picker style="width: 200px;" v-model="form.birthday"/>
         </a-form-item>
         <a-form-item label="身份证号码" field="idCard">
           <a-input v-model="form.idCard" placeholder="请输入身份证号码" />
         </a-form-item>
         <a-form-item label="联系方式" field="contact">
           <a-input v-model="form.contact" placeholder="请输入联系方式" />
         </a-form-item>
         <a-form-item label="户籍" field="hukouAddress">
           <a-input v-model="form.hukouAddress" placeholder="请输入户籍"/>
         </a-form-item>
         <a-form-item label="人口类型" field="populationType">
           <a-select v-model="form.populationType" placeholder="请选择">
             <a-option
                 v-for="dict in bis_peo_type"
                 :key="dict.value"
                 :label="dict.label"
                 :value="dict.value"
             ></a-option>
           </a-select>
         </a-form-item>
         <a-form-item label="备注" field="remark">
           <a-textarea v-model="form.remark" placeholder="请输入内容"></a-textarea>
         </a-form-item>
       </a-form>
     </a-drawer>
   </div>
</template>

<script setup name="Data">
import {getBisPopulation,getBisPopulationInfoList,addBisPopulationInfo,updateBisPopulationInfo,delBisPopulationInfo} from "@/api/grid/housekeeper";
import {getCurrentInstance, reactive, ref, toRefs} from "vue";
import {useRoute} from "vue-router";

const { proxy } = getCurrentInstance();
const { bis_peo_type } = proxy.useDict("bis_peo_type");
const { sys_user_sex } = proxy.useDict("sys_user_sex");

const dataList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const total = ref(0);
const title = ref("");
const route = useRoute();
const dictDrawerVisible = ref(false);
const direction = ref('rtl')

const data = reactive({
  form: {
    familyId: route.params.dictId,
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
    hukouAddress: undefined,
    familyId: route.params.dictId,
  },
  rules: {
    name: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
    gender: [{ required: true, message: "性别不能为空", trigger: "blur" }],
    relation: [{ required: true, message: "与户主关系不能为空", trigger: "blur" }],
    birthday: [{ required: true, message: "出生日期不能为空", trigger: "blur" }],
    idCard: [{ required: true, message: "身份证号码不能为空", trigger: "blur" }],
    contact: [{ required: true, message: "联系方式不能为空", trigger: "blur" }],
    hukouAddress: [{ required: true, message: "户籍不能为空", trigger: "blur" }],
    populationType: [{ required: true, message: "人口类型不能为空", trigger: "blur" }]
  }
});

const { queryParams, form, rules } = toRefs(data);

const handleDictOk = () => {
  submitForm()
};
const handleDictCancel = () => {
  dictDrawerVisible.value = false;
}

const onPageChange = (current) => {
  queryParams.value.pageNum = current
  getList()
};
const onPageSizeChange = (pageSize) => {
  queryParams.value.pageSize = pageSize
  getList()
};

/** 开启表格多选  */
const selectedIds = ref([]);
const rowSelection = reactive({
  type: 'checkbox',
  showCheckedAll: true,
  onlyCurrent: false,
});
// 修改按钮生效
const single = computed(() => {
  return selectedIds.value.length !== 1
});
// 删除按钮生效
const multiple = computed(() => {
  return !selectedIds.value.length
});

/** 查询字典数据列表 */
function getList() {
  loading.value = true;
  getBisPopulationInfoList(queryParams.value).then(response => {
    dataList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
/** 取消按钮 */
function cancel() {
  dictDrawerVisible.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {
    name: undefined,
    hukouAddress: undefined,
  };
  proxy.resetForm("dataRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 返回按钮操作 */
function handleClose() {
  const obj = { path: "/grid/housekeeper" };
  proxy.$tab.closeOpenPage(obj);
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  dictDrawerVisible.value = true;
  title.value = "添加人员数据";
  form.value.dictType = queryParams.value.dictType;
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const id = row.id || ids.value;
  getBisPopulation(id).then(response => {
    form.value = response.data;
    dictDrawerVisible.value = true;
    title.value = "修改人员数据";
  });
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["dataRef"].validate(valid => {
    if (!valid) {
      if (form.value.id != undefined) {
        updateBisPopulationInfo(form.value).then(response => {
          proxy.$message.success("修改成功");
          dictDrawerVisible.value = false;
          getList();
        });
      } else {
        form.value.familyId =  route.params.dictId
        addBisPopulationInfo(form.value).then(response => {
          proxy.$message.success("新增成功");
          dictDrawerVisible.value = false;
          getList();
        });
      }
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const pIds = row.id || ids.value;
  proxy.$confirm('是否确认删除编号为"' + pIds + '"的数据项？').then(function() {
    return delBisPopulationInfo(pIds);
  }).then(() => {
    getList();
    proxy.$message.success("删除成功");
  }).catch(() => {});
}
getList();
</script>
